if (window.plus) {
	ready();
} else {
	document.addEventListener('plusready', ready, false);
}

$(function() {
	if (AGENT_CLIENT.WEB) {
		ready();
	}
})

function ready() {
	var html = [];
	var thisPage = null;
	var user = null;
	var orderid = getUrlParam('orderid');
	var mainorderno = getUrlParam('mainorderno');
	var requestUrl = "";
	var paySuccess = function() {
		this.init();
	};

	paySuccess.prototype = {
		init: function() {
			this.initData();
			this.bindEvent();
		},
		initData: function() {
			var thisModel = this;
			user = getLocalStorage(STORAGE_MANAGER.user);
			if (!user) {
				redirect("../account/login.html");
				return;
			}
			if (!mainorderno) {
				$.toast('没有找到订单号');
				return;
			}
			requestUrl = CONFIG_MANAGER.RequstUrl + "v1/orders/main/" + mainorderno + "/" + user.id;
			$.ajax({
				type: "GET",
				url: requestUrl,
				async: true,
				contentType: 'application/json',
				headers: {
					"token": user.token
				},
				beforeSend:function(){ $.showWaiting(); }
			}).done(function(data) {
				thisModel.initPage(data);
			}).fail(function(data) {
				$.toast(data.responseJSON.Message);
			}).always(function(data) { $.closeWaiting(); })
		},
		//初始化页面
		initPage: function(orders) {			
			html = [];
			var order = orders[0];
			html.push('<div class="zcchengg">');
			html.push('<p class="zccgdui"><img src="../../images/zccgdui.png"></p>');
			html.push('<p class="gongxi"><span>恭喜！支付成功</span>');
			html.push('<br>您的包裹将整装待发，交易成功后将获得星币奖励！</p>');
			html.push('</div>');
			html.push('<div class="zfcg-dizhi mbtopbottom">');
			html.push('<div class="zfcg-k">');
			html.push('<p class="zfcg-bt1">您的订单将送往如下地址，如需修改请点击我的订单。</p>');
			html.push('<p class="zfcg-bt1 zfcg-bt2"><span>'+ order.mobile +'</span>收件人：' + order.consignee + '</p>');
			html.push('<p class="zfcg-bt1">' + order.region_name + ',' + order.address + '</p>');
			html.push('<p class="zfcg-bt1">备注：' + order.remark + '</p>');
			html.push('</div>');
			html.push('</div>');
			html.push('<div class="zfcg-dizhi mbtopbottom">');

			var i = 0;
			var totalCoin = 0;
			var totalPayfee = 0;
			var totalDeliveryfee  = 0;
			while (orders[i]) {
				html.push('<div class="zfcg-k mbbottom">');
				html.push('<p class="zfcg-bt1 zfcg-bt2"><span>网上支付</span>订单' + (i + 1) + '：' + orders[i].order_no + '</p>');
				var items = orders[i].items;
				var j = 0;
				while (items[j]){
					html.push('<p class="zfcg-bt1">商品名称：' + items[j].sku_name + '</p>');
					j++;
				}				
				html.push('<p class="zfcg-bt1">支付金额：<span class="money hongse">&yen;' + orders[i].pay_fee + '</span>+<span class="jinbi"><b><img src="../../images/jbtub.jpg"></b>' + orders[i].ttl_coin + '</span></p>');
				html.push('</div>');
				totalCoin += orders[i].ttl_coin;
				totalPayfee += orders[i].pay_fee;
				totalDeliveryfee += orders[i].delivery_fee
				i++;
			}
			html.push('<div class="zfcg-sfjek">');
			html.push('<p class="zfcg-sfje">实付金额：<span class="money hongse">&yen;' + (totalPayfee) + '</span>+<span class="jinbi"><b><img src="../../images/jbtub.jpg"></b>' + totalCoin + '</span></p>');
			html.push('<p class="zfcg-hyf">含运费: ' + totalDeliveryfee + '</p>');
			html.push('</div>');
			html.push('</div>');
			html.push('<p id="viewOrder" class="sjqyan">查看订单</p>');
			html.push('<p id="return_indexpage" class="sjbban">返回首页</p>');
			$("#main").html(html.join(''));
		},
		bindEvent: function() {
			//回退
			$("#back").on("click", function() {
				if (AGENT_CLIENT.APP) {
					plus.webview.getWebviewById("../mine/mylife.html").evalJS("reloadPage()");
					plus.webview.getWebviewById("../home/main.html").evalJS("redirectTab(0)");					
				} else{
					redirect("../home/index.html");
				}
			});
			//查看订单
			$("#main").on("click","#viewOrder", function() {
				redirect("../order/detail.html?mainorderno=" + mainorderno);
			});
			//返回首页
			$("#main").on("click","#return_indexpage", function() {
				if(AGENT_CLIENT.APP){
					plus.webview.getWebviewById("../home/main.html").evalJS("redirectTab(0)");
				}else{
					redirect("../home/index.html");
				}
			});
		}
	};
	thisPage = new paySuccess();
}